#!/bin/sh

# https://codeberg.org/river/wiki

# This is the example configuration file for river.
#
# If you wish to edit this, you will probably want to copy it to
# $XDG_CONFIG_HOME/river/init or $HOME/.config/river/init first.
#
# See the river(1), riverctl(1), and rivercarro(1) man pages for complete
# documentation.

# Note: the "Super" modifier is also known as Logo, GUI, Windows, Mod4, etc.

# Super+Shift+Return to start an instance of foot (https://codeberg.org/dnkl/foot)
riverctl map normal Super Z spawn "openthe terminal"
riverctl map normal Super X spawn "openthe browser"
riverctl map normal Super P spawn "openthe keepassx"
riverctl map normal Super Q spawn "openthe qq"
riverctl map normal Super A spawn "openthe wechat"
riverctl map normal Super Space spawn "openthe dictionary"
riverctl map normal None XF86XK_Suspend spawn "$HOME/.local/bin/suspend.sh"
riverctl map normal Alt D spawn "openthe menu"

# Super+Q to close the focused view
riverctl map normal Alt V close

# Super+Shift+E to exit river
riverctl map normal Alt+Shift Q exit

# Super+J and Super+K to focus the next/previous view in the layout stack
riverctl map normal Alt J focus-view next
riverctl map normal Alt K focus-view previous

# Super+Shift+J and Super+Shift+K to swap the focused view with the next/previous
# view in the layout stack
riverctl map normal Alt+Shift J swap next
riverctl map normal Alt+Shift K swap previous

# Super+Period and Super+Comma to focus the next/previous output
riverctl map normal Alt Period focus-output next
riverctl map normal Alt Comma focus-output previous

# Super+Shift+{Period,Comma} to send the focused view to the next/previous output
riverctl map normal Alt+Shift Period send-to-output next
riverctl map normal Alt+Shift Comma send-to-output previous

# Super+Return to bump the focused view to the top of the layout stack
riverctl map normal Alt Return zoom

# Super+Alt+{H,J,K,L} to move views
riverctl map normal Alt+Super H move left 100
riverctl map normal Alt+Super J move down 100
riverctl map normal Alt+Super K move up 100
riverctl map normal Alt+Super L move right 100

# Super+Alt+Control+{H,J,K,L} to snap views to screen edges
riverctl map normal Alt+Super+Control H snap left
riverctl map normal Alt+Super+Control J snap down
riverctl map normal Alt+Super+Control K snap up
riverctl map normal Alt+Super+Control L snap right

# Super+Alt+Shift+{H,J,K,L} to resize views
riverctl map normal Alt+Super+Shift H resize horizontal -100
riverctl map normal Alt+Super+Shift K resize vertical 100
riverctl map normal Alt+Super+Shift J resize vertical -100
riverctl map normal Alt+Super+Shift L resize horizontal 100

# Super + Left Mouse Button to move views
riverctl map-pointer normal Alt BTN_LEFT move-view

# Super + Right Mouse Button to resize views
riverctl map-pointer normal Alt BTN_RIGHT resize-view

# Super + Middle Mouse Button to toggle float
riverctl map-pointer normal Alt+Shift BTN_MIDDLE toggle-float

for i in $(seq 1 9)
do
    tags=$((1 << ($i - 1)))

    # Super+[1-9] to focus tag [0-8]
    riverctl map normal Alt $i set-focused-tags $tags

    # Super+Shift+[1-9] to tag focused view with tag [0-8]
    riverctl map normal Alt+Shift $i set-view-tags $tags

    # Super+Control+[1-9] to toggle focus of tag [0-8]
    riverctl map normal Alt+Control $i toggle-focused-tags $tags

    # Super+Shift+Control+[1-9] to toggle tag [0-8] of focused view
    riverctl map normal Alt+Shift+Control $i toggle-view-tags $tags
done

scratch_tag=$((1 << 20))

# Toggle the scratchpad with Super+P
riverctl map normal Alt N toggle-focused-tags ${scratch_tag}
# Send windows to the scratchpad with Super+Shift+P
riverctl map normal Alt+Shift N set-view-tags ${scratch_tag}

# Super+0 to focus all tags
# Super+Shift+0 to tag focused view with all tags
all_but_scratch_tags=$(( ((1 << 32) - 1) ^ $scratch_tag ))
riverctl spawn-tagmask ${all_but_scratch_tags}
riverctl map normal Alt 0 set-focused-tags $all_but_scratch_tags
riverctl map normal Alt+Shift 0 set-view-tags $all_but_scratch_tags

# Super+Space to toggle float
riverctl map normal Alt+Shift Space toggle-float

# Super+F to toggle fullscreen
riverctl map normal Alt F toggle-fullscreen

riverctl map normal Alt M  send-layout-cmd wideriver "--layout monocle"
riverctl map normal Alt T  send-layout-cmd wideriver "--layout left"

riverctl map normal Alt up  send-layout-cmd wideriver "--layout monocle"
riverctl map normal Alt left  send-layout-cmd wideriver "--layout left"
riverctl map normal Alt down  send-layout-cmd wideriver "--layout wide"
riverctl map normal Alt right send-layout-cmd wideriver "--layout right"

riverctl map normal Alt L  send-layout-cmd wideriver "--ratio +0.025"
riverctl map normal Alt H send-layout-cmd wideriver "--ratio -0.025"

riverctl map normal Alt+Shift H  send-layout-cmd wideriver "--count +1"
riverctl map normal Alt+Shift L send-layout-cmd wideriver "--count -1"

riverctl map normal Alt e     send-layout-cmd wideriver "--stack even"
riverctl map normal Alt w     send-layout-cmd wideriver "--stack dwindle"
riverctl map normal Alt i     send-layout-cmd wideriver "--stack diminish"

# Declare a passthrough mode. This mode has only a single mapping to return to
# normal mode. This makes it useful for testing a nested wayland compositor
riverctl declare-mode passthrough

# Super+F11 to enter passthrough mode
riverctl map normal Alt F11 enter-mode passthrough

# Super+F11 to return to normal mode
riverctl map passthrough Alt F11 enter-mode normal

# Various media key mapping examples for both normal and locked mode which do
# not have a modifier
for mode in normal locked
do
    # Eject the optical drive (well if you still have one that is)
    riverctl map $mode None XF86Eject spawn 'eject -T'

    # Control pulse audio volume with pamixer (https://github.com/cdemoulins/pamixer)
    riverctl map $mode None XF86AudioRaiseVolume  spawn "$HOME/.local/bin/set-volume 1"
    riverctl map $mode None XF86AudioLowerVolume  spawn "$HOME/.local/bin/set-volume -1"
    riverctl map $mode None XF86AudioMute         spawn "$HOME/.local/bin/set-volume 0"

    # Control MPRIS aware media players with playerctl (https://github.com/altdesktop/playerctl)
    riverctl map $mode None XF86AudioMedia spawn 'playerctl play-pause'
    riverctl map $mode None XF86AudioPlay  spawn 'playerctl play-pause'
    riverctl map $mode None XF86AudioPrev  spawn 'playerctl previous'
    riverctl map $mode None XF86AudioNext  spawn 'playerctl next'

    # Control screen backlight brightness with brightnessctl (https://github.com/Hummer12007/brightnessctl)
    riverctl map $mode None XF86MonBrightnessUp   spawn "brightnessctl set +10%"
    riverctl map $mode None XF86MonBrightnessDown spawn "brightnessctl set 10%-"
done

riverctl focus-follows-cursor always

# Set background and border color
riverctl background-color 0x002b36
riverctl border-color-focused 0xFFFF00
riverctl border-color-unfocused 0x000000

# Set keyboard repeat rate
riverctl set-repeat 50 300

# Make all views with an app-id that starts with "float" and title "foo" start floating.
# riverctl rule-add -app-id 'float*' -title 'foo' float

# Make all views with app-id "bar" and any title use client-side decorations
riverctl rule-add -app-id "bar" csd

"$HOME"/.local/bin/startw

# set layout manager
# https://github.com/alex-courtis/wideriver
riverctl default-layout wideriver

# start layout manager
wideriver \
    --layout                       left        \
    --layout-alt                   monocle     \
    --stack                        even        \
    --count-master                 1           \
    --ratio-master                 0.55        \
    --count-wide-left              0           \
    --ratio-wide                   0.35        \
    --border-width                 2           \
    --border-width-monocle         0           \
    --border-color-focused         "0xffff00"  \
    --border-color-focused-monocle "0x586e75"  \
    --border-color-unfocused       "0x000000"  \
    --log-threshold                info        \
   > "/tmp/wideriver.${XDG_VTNR}.${USER}.log" 2>&1 &
